/*
 * Copyright 2014 trinea.cn All right reserved. This software is the confidential and proprietary information of
 * trinea.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with trinea.cn.
 */
package com.movecar.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.movecar.model.AdPicBean;
import com.movecar.ui.ClubDetailActivity;
import com.movecar.ui.GroupDetailActivity;
import com.movecar.ui.ThemeDetailWaterFallActivity;
import com.movecar.utils.AppConfig;
import com.movecar.utils.ListUtils;
import com.movecar.utils.imageloader.Constants;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * ImagePagerAdapter
 * 
 * @author Zhao.JQ
 */
public class HomeAdImagePagerAdapter extends RecyclingPagerAdapter {

	private Activity activity;
	private List<AdPicBean> imageList;

	private int size;
	private boolean isInfiniteLoop;
	private ImageLoader imageLoader;

	private boolean click;

	public HomeAdImagePagerAdapter(Activity activity, List<AdPicBean> imageList, boolean click) {
		this.activity = activity;
		this.imageList = imageList;
		this.click = click;
		this.size = ListUtils.getSize(imageList);
		isInfiniteLoop = false;
		imageLoader = ImageLoader.getInstance();

	}

	@Override
	public int getCount() {
		// Infinite loop
		return isInfiniteLoop ? Integer.MAX_VALUE : ListUtils.getSize(imageList);
	}

	/**
	 * get really position
	 * 
	 * @param position
	 * @return
	 */
	private int getPosition(int position) {
		return isInfiniteLoop ? position % size : position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup container) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = holder.imageView = new ImageView(activity);

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		holder.imageView.setScaleType(ScaleType.FIT_XY);
		imageLoader.displayImage(AppConfig.HOST + imageList.get(getPosition(position)).getLogo(), holder.imageView,
				Constants.image_display_options, AppConfig.animateListener);
		holder.imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (imageList != null && click) {
					if ("0".equals(imageList.get(getPosition(position)).getState())) {

					} else if ("1".equals(imageList.get(getPosition(position)).getState())) {
						Intent intent = new Intent().setClass(activity, GroupDetailActivity.class);
						intent.putExtra("messageId", imageList.get(getPosition(position)).getRelateId());
						activity.startActivity(intent);

					} else if ("2".equals(imageList.get(getPosition(position)).getState())) {
						Intent intent = new Intent().setClass(activity, ClubDetailActivity.class);
						intent.putExtra("sectionId", imageList.get(getPosition(position)).getRelateId());
						activity.startActivity(intent);

					} else if ("3".equals(imageList.get(getPosition(position)).getState())) {
						Intent intent = new Intent().setClass(activity, ThemeDetailWaterFallActivity.class);
						intent.putExtra("ThemeId", imageList.get(getPosition(position)).getRelateId());
						intent.putExtra("detailLogo", imageList.get(getPosition(position)).getDetailLogo());
						intent.putExtra("isRelate", imageList.get(getPosition(position)).getIsRelate());
						activity.startActivity(intent);

					} else {

					}

				}

			}
		});

		return view;
	}

	private static class ViewHolder {

		ImageView imageView;
	}

	/**
	 * @return the isInfiniteLoop
	 */
	public boolean isInfiniteLoop() {
		return isInfiniteLoop;
	}

	/**
	 * @param isInfiniteLoop
	 *            the isInfiniteLoop to set
	 */
	public HomeAdImagePagerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
		this.isInfiniteLoop = isInfiniteLoop;
		return this;
	}

}
